/**
 * 
 */
package com.yidu.view;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yidu.bean.TbImage;
import com.yidu.bean.TbMember;
import com.yidu.bean.TbOrder;
import com.yidu.bean.TbOrderCare;
import com.yidu.bean.TbShop;
import com.yidu.bean.TbShopping;
import com.yidu.bean.TbSite;
import com.yidu.commom.Tool;
import com.yidu.message.Message;
import com.yidu.message.Pages;
import com.yidu.service.TbOrderCareService;
import com.yidu.service.TbOrderService;
import com.yidu.service.TbShoppingService;

/**
 * 类的描述 -- 类
 * @author LiuZongNan
 * 2018年7月25日
 */
@RequestMapping("/order")
@Controller
public class TbOrderAction {
	//控制台打印
	Logger logger = Logger.getLogger(TbSiteAction.class);
	
	//消息信息类
	Message msg = new Message();

	//json
	ObjectMapper mapper = new ObjectMapper();
	@Resource
	private TbOrderService orderServiceImpl;
	@Resource
	private TbShoppingService shoppingService;
	@Resource
	private TbOrderCareService orderCareService;
	@RequestMapping("/tiao")
	public String shoe(){
		logger.info("进入店家表...");
		return "background/center/details";
	}
	@RequestMapping("/tiaos")
	public String shoes(String orderId){
		
		//session.setAttribute("orderId", orderId);,HttpSession session
		//logger.info("进入店家表..."+orderId);
		return "background/center/details";
	}
	/**
	 * 增加和修改
	 * @param session web存储空间
	 * @param me 菜单对象
	 * @return 页面跳转
	 */
	@RequestMapping("add")
	@ResponseBody
	public Message add(HttpSession session,TbOrder order) {
		System.out.println("wwwwwwwwwwwwwwwww"+order.getOrderState()+"wwwwwwwwwwwwwwwww"+order.getOrderMoney());
		TbMember member = (TbMember) session.getAttribute("user");
		String shopId =  (String) session.getAttribute("shop");
		TbOrder orders = new TbOrder();
		orders.setOrderId(UUID.randomUUID().toString());
		orders.setShopId(shopId);
		System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAA"+shopId);
		orders.setMemId(member.getMemId());
		Date date = new Date();
		orders.setOrderTime(date);
		orders.setOrderState(order.getOrderState());
		orders.setSort(1);
		orders.setOrderMoney(order.getOrderMoney());
		orders.setIsva(0);
		int add = orderServiceImpl.save(orders);
		List<TbShopping> list = shoppingService.findByMemId(member.getMemId());
		
		for (TbShopping tbShopping : list) {
			
			TbOrderCare orderCare = new TbOrderCare();
			orderCare.setOrderId(orders.getOrderId());
			orderCare.setCareId(UUID.randomUUID().toString());
			orderCare.setCareMoney(tbShopping.getMoney());
			orderCare.setMenuId(tbShopping.getMenuId());
			orderCare.setIsva(1);
			orderCare.setCareNum(tbShopping.getSopNumber());
			orderCare.setCareMoney(tbShopping.getMoney());
			int adds = orderCareService.save(orderCare);
			
		}
		if(add>0){
			msg.setMsg("增加成功");
			msg.setNum(1);
			return msg;
		}else{
			msg.setMsg("增加失败");
			msg.setNum(0);
			return msg;
		}
	}
	/*@RequestMapping("/orderAll")
	@ResponseBody
	public Object findAll(HttpServletResponse response,Pages pages,TbOrder order) {
		response.setCharacterEncoding("UTF-8");
		List<TbOrder> list = orderServiceImpl.findAllPage(pages, order);
		for(int i=0; i < list.size();i++){
			TbOrder orders = list.get(i);
			list.set(i, orders);
		}
		Long longs = orderServiceImpl.findCount(order);
		int row = longs.intValue();
		Pages pages = new Pages();
		pages.setPage(page);
		pages.setTotalRows(limit);
		Map<String,String> map=new HashMap<>();
		System.out.println(map);
		map.put("memName", order.getMemName());
		map.put("start", String.valueOf(pages.getStart()));
		map.put("rows", String.valueOf(pages.getTotalRows()));
		List<TbOrder> list = orderServiceImpl.findAllPage(map);
		Map<String, Object> maps = new HashMap<>();
		//需要符合layUI格式，code msg count data
		maps.put("code", 0);
		maps.put("msg", "1");
		maps.put("count", row);
		maps.put("data", list);
		//order=new TbOrder();
		return maps;
	}*/
	/**
	 * 查询所有和条件查询分页
	 * @param response 响应
	 * @param me 菜单对象
	 * @param page 页数
	 * @param limit 行数
	 * @return
	 */
	@RequestMapping("/orderAll")
	@ResponseBody
	public Map<String, Object> findAll(HttpServletResponse response,TbOrder order,int page,int limit) {
		response.setCharacterEncoding("UTF-8");
		Pages pages = new Pages();
		pages.setPage(page);
		pages.setTotalRows(limit);
		Map<String,String> map=new HashMap<>();
		map.put("memName", order.getMemName());
		map.put("start", String.valueOf(pages.getStart()));
		map.put("rows", String.valueOf(pages.getTotalRows()));
		List<TbOrder> list = orderServiceImpl.findAllPage(map);
		Map<String, Object> maps = new HashMap<>();
		//需要符合layUI格式，code msg count data
		maps.put("code", 0);
		maps.put("msg", "1");
		maps.put("count", orderServiceImpl.findTotalRows(order));
		maps.put("data", list);
		order=new TbOrder();
		return maps;
	}
	/*@RequestMapping("/findAllOer")
	@ResponseBody
	public List<TbOrder> findAllOer(HttpSession session,String memId){
		session = (HttpSession) request.getAttribute("user");
		System.out.println("dad dmsadnsaidn"+session);
		TbMember member = (TbMember) session.getAttribute("user");
		System.out.println("22222222"+member.getMemId());
		memId = member.getMemId();
		logger.info("findAllOer:根据用户id查询订单的方法"+memId);
		List<TbOrder> order = orderServiceImpl.findAllOer(memId);
		return order;
	}*/
	@RequestMapping(value="/Update")
	@ResponseBody
	public Object Update(TbOrder order) throws IOException {
		
		System.out.println("123"+order.getOrderId());
	    //判断是否存在id 
		int num = 0;
 		if(!Tool.isEmpty(order.getOrderId())){
 			//存在 执行修改
 			order.setIsva(1);
 			order.setOper("admin");
 			num = orderServiceImpl.update(order);
 		}
		//根据 操作返回
		msg.setNum(num);
		if(num>0){
			msg.setMsg("操作成功!");
		}else{
			msg.setMsg("操作失败!");
		}
		return msg;
	}
	@RequestMapping("/findAllOer")
	@ResponseBody
	public List<TbOrder> findAllOer(HttpSession session , String memId){
		TbMember member = (TbMember) session.getAttribute("user");
		memId = member.getMemId();
		logger.info("findAllOer:根据用户id查询订单的方法"+memId);
		List<TbOrder> order = orderServiceImpl.findAllOer(memId);
		
		return order;
	}
	@RequestMapping("/findById")
	@ResponseBody
	public List<TbOrder> findById(String orderId){
		//String orderId = (String) session.getAttribute("orderId");
		logger.info("findAllOer:根据用户id查询订单的方法");
		System.out.println(orderId+"小豆子+++++++++++++++");
		List<TbOrder> orders = orderServiceImpl.findById(orderId);
		return orders;
	}
	/**
	 * 根据ID查询对象类型
	 * 
	 * @param id
	 * @param response
	 * @return
	 */
	@RequestMapping("/showUpdate")
	@ResponseBody
	public Object showUpdate(String orderId){
			logger.info("进入显示...");
			
			//HttpServletResponse response = Tools.getResponse();
			
			TbOrder order = orderServiceImpl.selectByPrimaryKey(orderId);
			
			//logger.info(gson.toJson(vo));
			
		 return order;
		  
	}
}
